﻿/* Missiles.cs
 * Author: Richard Broker
 * 
 *  This file is part of Tame.
 *
 *  Tame is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Tame is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Tame. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tame
{
    class Missiles
    {
        //http://forum.pardus.at/archive/index.php?showtopic=13388
        // Developed by Influence D, derived from Warbird's Pardus Bible formula by Baron using a Javascript Genetic Algorithm Engine
        // Ported to Visual C# by Richard Broker a.k.a Pigeon.
        // Error compared to manual: 0.025
        public double calcMissileHitChance(int missile_int, int ship_size, int evasion_bonus_eng, int evasion_bonus_ecm)
        {
            string dbg = "";
            try
            {
                var sqRSize = Math.Sqrt(ship_size);

                var base_val = 8 + (3.7215 * sqRSize);
                var step_val = 7.0481 + (2.8091 * sqRSize) + (-2.4899 * Math.Log10(ship_size));


                var v = (1 - evasion_bonus_eng) * ((missile_int * step_val) + base_val);
                if (evasion_bonus_ecm != 0)
                    v = (1 - evasion_bonus_ecm) * (v - Math.Max(0, (v - 100) / 2));
                return Math.Min(100, Math.Max(0, Math.Round(v)));
            }
            catch (Exception)
            {
                MessageBox.Show("calcMissileHitChance failed: " + dbg + "" + Environment.NewLine + "" + Environment.NewLine + "", "Alert");
            }
            return -1;
        }
    }
}
